namespace Collaboris.Saf.Actions.Moss.Tests.Generated
{
    using System;
    using System.Collections.ObjectModel;

    using Engine.Report;

    using Entities;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Publishing;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Moss.Publishing;

    using Saf.Tests;

    using SharePoint.Moss.Publishing;

    /// <summary>
    /// This test was originally generated but has now been tested and verfiied by MJ
    /// </summary>
    [TestClass]
    public class SetAvailableSiteTemplatesTest
    {
        #region Public Methods

        [TestMethod]
        public void SetAvailableSiteTemplates_Test()
        {
            var setavailablesitetemplates =
                new TestAssistant<SetAvailableSiteTemplates, SiteTemplate>(
                    TestEnvironment.PublishingSite, "SetAvailableSiteTemplatesTestWeb", "BLANKINTERNET#0");

            using (setavailablesitetemplates)
            {
                PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(setavailablesitetemplates.TestWeb);

                MacroReport doReport = setavailablesitetemplates.DoMacro();

                SPWebTemplateCollection availableTemplatesAfter = 
                    pubWeb.GetAvailableWebTemplates(setavailablesitetemplates.FirstEntity.LCID.Value);

                bool templateInAvaliableList = false;
                foreach (SPWebTemplate templatesAfter in availableTemplatesAfter)
                {
                    if (!templatesAfter.Name.Equals(setavailablesitetemplates.FirstEntity.Name, 
                        StringComparison.OrdinalIgnoreCase))
                        continue;
                    templateInAvaliableList = true;
                }

                Assert.IsTrue(templateInAvaliableList);

                MacroReport unDoReport = setavailablesitetemplates.UndoMacro();

                availableTemplatesAfter =
                    pubWeb.GetAvailableWebTemplates(setavailablesitetemplates.FirstEntity.LCID.Value);

                templateInAvaliableList = false;
                foreach (SPWebTemplate templatesAfter in availableTemplatesAfter)
                {
                    if (!templatesAfter.Name.Equals(setavailablesitetemplates.FirstEntity.Name,
                        StringComparison.OrdinalIgnoreCase))
                        continue;
                    templateInAvaliableList = true;
                }

                Assert.IsFalse(templateInAvaliableList);

            }
        }

        [TestMethod]
        public void TestGetSiteTemplate()
        {
            // This test expects the publishingSite with this template as available.
            const string templateName = "BLANKINTERNET#0";
            const uint lcid = 1033;

            // Test the Do method
            using (SPSite site = new SPSite(TestEnvironment.PublishingSite))
            {
                using (SPWeb web = site.RootWeb)
                {
                    PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
                    SiteTemplateHelper helper = new SiteTemplateHelper();
                    SPWebTemplate webTemplate = SiteTemplateHelper.GetSiteTemplate(pubWeb, templateName, lcid);

                    Assert.AreEqual(webTemplate.Name, templateName);
                }
            }
        }

        [TestMethod]
        public void TestSetAsAvailable()
        {
            // This test expects the publishingSite with this template as available.
            const string templateName = "BLANKINTERNET#0";
            const uint lcid = 1033;

            // Test the Do method
            using (SPSite site = new SPSite(TestEnvironment.PublishingSite))
            {
                using (SPWeb web = site.RootWeb)
                {
                    PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);

                    SiteTemplateHelper helper = new SiteTemplateHelper();

                    SPWebTemplateCollection availableTemplatesBefore = pubWeb.GetAvailableWebTemplates(lcid);

                    Collection<SPWebTemplate> webTemplateCollection = new Collection<SPWebTemplate>();
                    SPWebTemplate publishMinimalwebTemplate = SiteTemplateHelper.GetSiteTemplate(pubWeb, templateName, lcid);
                    webTemplateCollection.Add(publishMinimalwebTemplate);

                    SiteTemplateHelper.SetAsAvailable(pubWeb, webTemplateCollection, lcid, true);

                    SPWebTemplateCollection availableTemplatesAfter = pubWeb.GetAvailableWebTemplates(lcid);

                    bool templateInAvaliableList = false;
                    foreach (SPWebTemplate templatesAfter in availableTemplatesAfter)
                    {
                        if (!templatesAfter.Name.Equals(templateName, StringComparison.OrdinalIgnoreCase))
                            continue;
                        templateInAvaliableList = true;
                    }

                    Assert.IsTrue(templateInAvaliableList);
                }
            }
        }

        [TestMethod]
        public void TestSetAsUnAvailable()
        {
            // This test expects the publishingSite with this template as available.
            const string templateName = "BLANKINTERNET#0";
            const uint lcid = 1033;

            // Test the Do method
            using (SPSite site = new SPSite(TestEnvironment.PublishingSite))
            {
                using (SPWeb web = site.RootWeb)
                {
                    PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);

                    SiteTemplateHelper helper = new SiteTemplateHelper();

                    SPWebTemplateCollection availableTemplatesBefore = pubWeb.GetAvailableWebTemplates(lcid);

                    Collection<SPWebTemplate> webTemplateCollection = new Collection<SPWebTemplate>();
                    SPWebTemplate publishMinimalwebTemplate = SiteTemplateHelper.GetSiteTemplate(pubWeb, templateName, lcid);
                    webTemplateCollection.Add(publishMinimalwebTemplate);

                    SiteTemplateHelper.SetAsUnAvailable(pubWeb, webTemplateCollection, lcid, true);

                    SPWebTemplateCollection availableTemplatesAfter = pubWeb.GetAvailableWebTemplates(lcid);

                    bool templateInAvaliableList = false;
                    foreach (SPWebTemplate templatesAfter in availableTemplatesAfter)
                    {
                        if (!templatesAfter.Name.Equals(templateName, StringComparison.OrdinalIgnoreCase))
                            continue;
                        templateInAvaliableList = true;
                    }

                    Assert.IsFalse(templateInAvaliableList);
                }
            }
        }


        #endregion
    }
}